package com.thomasmoerman.dci2.pbc

import com.thomasmoerman.dci2.frw._
import com.thomasmoerman.dci2.tmc._
import com.thomasmoerman.dci2.domain._

trait Debtor {

  def payBills

}

trait DebtorProvider {
 
  implicit def asDebtor(person: Person): Debtor
  
}

trait TransactionalDebtorProvider extends DebtorProvider {
  this: Transactional with PayBillsQueries with TransferMoneyContextFactory =>

  implicit def asDebtor(person: Person): Debtor = new TransactionalDebtor(person)
  
  class TransactionalDebtor(self: Person) extends Debtor {
	
    def payBills = inTransaction {
      findUnpaidBills(self.id) foreach {case (bill, creditor) =>
    	new TransferMoneyContext(self.accountId, creditor.accountId, bill.amount).execute
    	markAsPaid(bill.id)
      }
    }
	  
  }
  
}